package cn.doitedu.hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.MD5Hash;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 本mapreduce，需要保证 输出的  kv，要严格全局有序：
 *
 */
public class HFileProcessor {

    public static class HFileProcessorMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>{

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context) throws IOException, InterruptedException {
            // 000046,GxrgmE,33,female,北京,26505,2017-01-14
            String[] split = value.toString().split(",");
            String rkMd5 = MD5Hash.getMD5AsHex(Bytes.toBytes(split[0]));

            // 将这一行原始数据拆分成了6个KeyValue输出
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rkMd5)),new KeyValue(Bytes.toBytes(rkMd5),Bytes.toBytes("base_info"),Bytes.toBytes("age"),Bytes.toBytes(Integer.parseInt(split[2]))));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rkMd5)),new KeyValue(Bytes.toBytes(rkMd5),Bytes.toBytes("base_info"),Bytes.toBytes("gender"),Bytes.toBytes(split[3])));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rkMd5)),new KeyValue(Bytes.toBytes(rkMd5),Bytes.toBytes("base_info"),Bytes.toBytes("name"),Bytes.toBytes(split[1])));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rkMd5)),new KeyValue(Bytes.toBytes(rkMd5),Bytes.toBytes("extra_info"),Bytes.toBytes("city"),Bytes.toBytes(split[4])));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rkMd5)),new KeyValue(Bytes.toBytes(rkMd5),Bytes.toBytes("extra_info"),Bytes.toBytes("graduate"),Bytes.toBytes(split[6])));
            context.write(new ImmutableBytesWritable(Bytes.toBytes(rkMd5)),new KeyValue(Bytes.toBytes(rkMd5),Bytes.toBytes("extra_info"),Bytes.toBytes("salary"),Bytes.toBytes(Integer.parseInt(split[5]))));

        }
    }

    public static void main(String[] args) throws Exception {

        Configuration conf = HBaseConfiguration.create();
        Job job = Job.getInstance(conf);

        job.setJarByClass(HFileProcessor.class);

        job.setMapperClass(HFileProcessorMapper.class);

        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        job.setOutputKeyClass(ImmutableBytesWritable.class);
        job.setOutputValueClass(KeyValue.class);

        job.setNumReduceTasks(0);


        // 对HFileOutputFormat2类，做一些配置
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("doit:students");
        Table table = conn.getTable(tableName);
        RegionLocator regionLocator = conn.getRegionLocator(tableName);
        HFileOutputFormat2.configureIncrementalLoad(job,table,regionLocator);

        job.setOutputFormatClass(HFileOutputFormat2.class);

        FileInputFormat.setInputPaths(job,new Path("data/"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://doit01:8020/doit_students_hfiles/"));


        job.waitForCompletion(true);




    }
}
